package day20171008;

import java.util.Scanner;
public class Register {
    public static void main(String[] args) {
        //demon1();
        demon2();

    }

    //设置登录用户及登录密码，并限制登录次数
    public static void demon1() {
            Scanner sc = new Scanner(System.in);
            String name = "";    //保存用户名
            String pwd = "";     //保存密码
            int count = 0;        //记录登录次数
            while (!("admin".equals(name) && "1223".equals(pwd))) {
                //提示输入用户名
                System.out.println("请输入用户名");
                name = sc.next();
                //提示输入密码
                System.out.println("请输入密码");
                pwd = sc.next();

                //判断是否合法用户，若合法则退出循环
                if ("admin".equals(name) && "1223".equals(pwd)) {
                    System.out.println("登录成功");
                    break;
                }

                //统计用户输入的次数
                count++;

                //判断非法的次数是否已达上限
                if (count >= 3) {
                    System.out.println("登录次数已用完，请下次再来");
                    break;
                } else {
                    System.out.println("用户名或密码不正确，请重新输入。你还有" + (3 - count) + "次机会");
                }

            }
    }

    public static void demon2(){
        Scanner sc = new Scanner(System.in);
        String name = "";
        String pwd = "";

        for (int i=1;i<=3;i++){
            System.out.println("请输入用户名");
            name = sc.next();
            System.out.println("请输入密码");
            pwd = sc.next();

            if ("admin".equals(name) && "1223".equals(pwd)){
                System.out.println("登录成功");
                break;
            }else {
                System.out.println("用户名或密码不正确，请重新输入。你还有" + (3 - i) + "次机会");
                if ((3-i)==0){
                    System.out.println("登录次数已用完，请下次再来");
                }
            }
        }
    }


}
